package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.inn.UserDishes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class UserDishesDaoImpl extends TempDataObjectDao<UserDishes> {
    @Override
    protected boolean addData(UserDishes data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_inn_dishes(userId,dishesId,level,currentCookNum) VALUES(?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getDishesId());
            call.setInt(i++, data.getLevel());
            call.setInt(i++, data.getCurrentCookNum());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserDishes data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_inn_dishes SET level=?,currentCookNum=? WHERE userId=? and dishesId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getLevel());
            call.setInt(i++, data.getCurrentCookNum());

            call.setLong(i++,data.getUserId());
            call.setInt(i++,data.getDishesId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserDishes parseData(ResultSet rs) throws SQLException {
        UserDishes userDishes = new UserDishes();

        userDishes.setLevel(rs.getInt("level"));
        userDishes.setUserId(rs.getLong("userId"));
        userDishes.setCurrentCookNum(rs.getInt("currentCookNum"));
        userDishes.setDishesId(rs.getInt("dishesId"));
        return userDishes;
    }

    public Map<Integer, UserDishes> getUserDishesMap(long userId) {

        Map<Integer, UserDishes> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_inn_dishes where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserDishes data = parse(rs);
                dataMap.put(data.getDishesId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


}
